//
//  BBLegworkViewController.m
//  BearSiX
//
//  Created by BearBaby on 16/3/26.
//  Copyright © 2016年 Tom. All rights reserved.
//

#import "BBLegworkViewController.h"
#import "BBLegworkCell.h"
#import "BBTimePicker.h"
@interface BBLegworkViewController ()<UITableViewDelegate,UITableViewDataSource,UINavigationControllerDelegate,UIImagePickerControllerDelegate>
@property(nonatomic,strong)UITableView* tableView;
@property(nonatomic,strong)BBTimePicker* timePickerView;
@property(nonatomic,strong)UIDatePicker* datePicker;
@property(nonatomic,strong)UIButton* seleteBtn;
@property(nonatomic,strong)UIButton* cancelBtn;                  //取消按钮
@property(nonatomic,strong)UIButton* ensureBtn;                  //确定按钮
@property(nonatomic,assign)NSInteger tag;
@property(nonatomic,strong)NSString* startTime;                  //开始时间
@property(nonatomic,strong)NSString* endTime;                    //结束时间
@end

@implementation BBLegworkViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"外勤";
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"完成" style:UIBarButtonItemStylePlain target:self action:@selector(done:)];
 
    
    self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, WINSIZEWIDTH, WINSIZEHEIGHT) style:UITableViewStyleGrouped];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.view addSubview:self.tableView];
       [self initWithPickView];
}

#pragma mark 初始化选择时间视图
-(void)initWithPickView
{
    //弹出视图
    self.timePickerView = [[BBTimePicker alloc]initWithFrame:CGRectMake(0, WINSIZEHEIGHT, WINSIZEWIDTH, 276)];
    self.timePickerView.backgroundColor = [UIColor whiteColor];
    self.timePickerView.userInteractionEnabled = YES;
    _datePicker = [self.timePickerView viewWithTag:6003];
    _cancelBtn = (UIButton *)[self.timePickerView viewWithTag:6004];
    _ensureBtn = (UIButton *)[self.timePickerView viewWithTag:6005];
    [_ensureBtn addTarget:self action:@selector(sure:) forControlEvents:UIControlEventTouchUpInside];
    [_cancelBtn addTarget:self action:@selector(cancle:) forControlEvents:UIControlEventTouchUpInside];
    //[_datePicker addTarget:self action:@selector(chooseDate:) forControlEvents:UIControlEventValueChanged];
    [self.view addSubview:self.timePickerView];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 4;
    
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (section==0) {
        return 1;
    }else if (section==1){
        return 2;
    }else if(section==2){
        return 2;
    }
        return 1;
    
}



- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString* identifire = @"Cell";
    BBLegworkCell* cell = [self.tableView dequeueReusableCellWithIdentifier:identifire];
    if (indexPath.section==0) {
        
        if (!cell) {
            cell = [[BBLegworkCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifire];
            cell.delegate = self;
            cell.textLabel.font = [UIFont systemFontOfSize:WINSIZEWIDTH/25];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            [cell.addImage addTarget:self action:@selector(addImage:) forControlEvents:UIControlEventTouchUpInside];
        }
    }else if(indexPath.section==1){
        
        UITableViewCell* cell1 = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:nil];
        cell1.userInteractionEnabled = YES;
        if (indexPath.row==0) {
            cell1.textLabel.text = @"出发时间";
            cell1.detailTextLabel.text = self.startTime;
            self.seleteBtn = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, WINSIZEWIDTH, 44)];
            [self.seleteBtn setBackgroundColor:[UIColor clearColor]];
            self.seleteBtn.userInteractionEnabled = YES;
            self.seleteBtn.tag = 1;
            
            [self.seleteBtn addTarget:self action:@selector(seleteBtn:) forControlEvents:UIControlEventTouchUpInside];
        }else{
            cell1.textLabel.text = @"结束时间";
            cell1.detailTextLabel.text = self.endTime;
            self.seleteBtn = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, WINSIZEWIDTH, 44)];
            [self.seleteBtn setBackgroundColor:[UIColor clearColor]];
            self.seleteBtn.userInteractionEnabled = YES;
            self.seleteBtn.tag = 2;
            
            [self.seleteBtn addTarget:self action:@selector(seleteBtn:) forControlEvents:UIControlEventTouchUpInside];
            
        }
        [cell1 addSubview:self.seleteBtn];
        cell1.textLabel.font = [UIFont systemFontOfSize:WINSIZEWIDTH/25];
        cell1.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        
        
        
        return cell1;
        
        
        
    }else if (indexPath.section==2){
        UITableViewCell* cell2 = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:nil];
        if (indexPath.row==0) {
            cell2.textLabel.text = @"抵达时间";
        }else{
            cell2.textLabel.text = @"抵达地点";
            
        }
        cell2.textLabel.font = [UIFont systemFontOfSize:WINSIZEWIDTH/25];
        
        
        
        
        return cell2;
        
    }else if (indexPath.section==3){
        UITableViewCell* cell3 = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:nil];
        cell3.textLabel.text = @"提交到";
        cell3.textLabel.font = [UIFont systemFontOfSize:WINSIZEWIDTH/25];
        cell3.detailTextLabel.text = @"外勤";
        cell3.detailTextLabel.font = [UIFont systemFontOfSize:WINSIZEWIDTH/25];
        
        
        return cell3;
        
    }
    
    
    return cell;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section==0) {
        CGFloat cellHeight = 0.0f;
        BBLegworkCell* cell = (BBLegworkCell*)[self tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath];
        cellHeight = cell.finalH;
        return cellHeight;
    }else
        return 44;
    
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    if (section==0) {
        return 30;
    }else if (section==1){
        return 20;
    }
    return 0.000000001;
    
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    if (section==1) {
        return 30;
    }else
        return 15;
    
    
}
-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
    NSString* string = nil;
    if (section==1) {
        string = @"时间详情";
    }else if (section==2){
     string = @"抵达详情";
    }else{
        string = @"";
    }
    
    return string;
    
}
//添加图片
-(void)addImage:(UIButton*)sender{
    
    // 判断是否支持相机
    if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])
    {
        _sheet  = [[UIActionSheet alloc] initWithTitle:nil delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:nil otherButtonTitles:@"拍照",@"从相册选择"  , nil];
    }
    else
    {
        _sheet = [[UIActionSheet alloc] initWithTitle:nil delegate:self cancelButtonTitle:@"取消"  destructiveButtonTitle:nil otherButtonTitles:@"从相册选择", nil];
    }
    
    _sheet.tag = 255;
    
    [_sheet showInView:self.view];
    
    
}
#pragma mark - actionsheet delegate
-(void) actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (actionSheet.tag == 255)
    {
        NSUInteger sourceType = 0;
        
        // 判断是否支持相机
        if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])
        {
            switch (buttonIndex)
            {
                case 0:// 相机
                    sourceType = UIImagePickerControllerSourceTypeCamera;
                    break;
                case 1:// 相册
                    sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
                    break;
                case 2:
                    return;
                    break;
            }
        }
        else
        {
            if (buttonIndex == 0)
            {
                sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
            }
            else
            {
                return;
            }
        }
        
        if (sourceType == UIImagePickerControllerSourceTypePhotoLibrary || sourceType ==UIImagePickerControllerSourceTypeSavedPhotosAlbum)
        {
            
            
        }
        else if (sourceType == UIImagePickerControllerSourceTypeCamera)
        {
            // 跳转到相机或相册页面
            UIImagePickerController *imagePickerController = [[UIImagePickerController alloc] init];
            imagePickerController.delegate = self;
            imagePickerController.allowsEditing = YES;
            imagePickerController.sourceType = sourceType;
            [self presentViewController:imagePickerController animated:YES completion:^{}];
        }
    }
}
- (UIImage*)imageWithImageSimple:(UIImage*)image scaledToSize:(CGSize)newSize
{
    // Create a graphics image context
    UIGraphicsBeginImageContext(newSize);
    
    // Tell the old image to draw in this new context, with the desired
    // new size
    [image drawInRect:CGRectMake(0,0,newSize.width,newSize.height)];
    
    // Get the new image from the context
    UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
    
    // End the context
    UIGraphicsEndImageContext();
    
    // Return the new image.
    return newImage;
}
//时间弹框
-(void)seleteBtn:(UIButton*)sender{
    
   self.tag = sender.tag;
    if (sender.tag==1) {
        
       
        if (self.seleteBtn.selected == NO) {
            self.seleteBtn.selected = YES;
            [UIView animateWithDuration:0.3 animations:^{
                CGRect _smallHidden = self.timePickerView.frame;
                _smallHidden.origin.y = WINSIZEHEIGHT - _smallHidden.size.height;
                self.timePickerView.frame =_smallHidden;
            }
                             completion:^(BOOL finished)
             {
                 self.timePickerView.hidden = NO;
                 CGRect _smallHidden = self.timePickerView.frame;
                 _smallHidden.origin.y = WINSIZEHEIGHT - _smallHidden.size.height;
                 self.timePickerView.frame = _smallHidden;
             }];
            
            
        }else if (self.seleteBtn.selected == YES){
            self.seleteBtn.selected = NO;
            [UIView animateWithDuration:0.3 animations:^{
                CGRect _smallHidden = self.timePickerView.frame;
                _smallHidden.origin.y = 276 + _smallHidden.size.height;
                self.timePickerView.frame =_smallHidden;
            }
                             completion:^(BOOL finished)
             {
                 self.timePickerView.hidden = YES;
                 CGRect _smallHidden = self.timePickerView.frame;
                 _smallHidden.origin.y = 276 + _smallHidden.size.height;
                 self.timePickerView.frame = _smallHidden;
             }];
            
        }
        
    }else if (sender.tag==2){
        
        
        if (self.seleteBtn.selected == NO) {
            self.seleteBtn.selected = YES;
            [UIView animateWithDuration:0.3 animations:^{
                CGRect _smallHidden = self.timePickerView.frame;
                _smallHidden.origin.y = WINSIZEHEIGHT - _smallHidden.size.height;
                self.timePickerView.frame =_smallHidden;
            }
                             completion:^(BOOL finished)
             {
                 self.timePickerView.hidden = NO;
                 CGRect _smallHidden = self.timePickerView.frame;
                 _smallHidden.origin.y = WINSIZEHEIGHT - _smallHidden.size.height;
                 self.timePickerView.frame = _smallHidden;
             }];
            
            
        }else if (self.seleteBtn.selected == YES){
            self.seleteBtn.selected = NO;
            [UIView animateWithDuration:0.3 animations:^{
                CGRect _smallHidden = self.timePickerView.frame;
                _smallHidden.origin.y = 276 + _smallHidden.size.height;
                self.timePickerView.frame =_smallHidden;
            }
                             completion:^(BOOL finished)
             {
                 self.timePickerView.hidden = YES;
                 CGRect _smallHidden = self.timePickerView.frame;
                 _smallHidden.origin.y = 276 + _smallHidden.size.height;
                 self.timePickerView.frame = _smallHidden;
             }];
            
        }
        
        
    }
}

//取消按钮
-(void)cancle:(UIButton*)sender{
    self.seleteBtn.selected = !self.seleteBtn.selected;
    [UIView animateWithDuration:0.3 animations:^{
        CGRect _smallHidden = self.timePickerView.frame;
        _smallHidden.origin.y = 276 + _smallHidden.size.height;
        self.timePickerView.frame =_smallHidden;
    }
                     completion:^(BOOL finished)
     {
         self.timePickerView.hidden = YES;
         CGRect _smallHidden = self.timePickerView.frame;
         _smallHidden.origin.y = 276 + _smallHidden.size.height;
         self.timePickerView.frame = _smallHidden;
     }];
    


}
//确定按钮
-(void)sure:(UIButton*)sender{

    NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
    [dateFormat setDateFormat:@"yyyy-MM-dd"];
    NSDate* date = _datePicker.date;
    
    if (self.tag==1) {
        self.startTime = [dateFormat stringFromDate:date];
    }else if (self.tag==2){
        
        self.endTime = [dateFormat stringFromDate:date];
    }
    
    self.seleteBtn.selected = !self.seleteBtn.selected;
    [UIView animateWithDuration:0.3 animations:^{
        CGRect _smallHidden = self.timePickerView.frame;
        _smallHidden.origin.y = 276 + _smallHidden.size.height;
        self.timePickerView.frame =_smallHidden;
    }
                     completion:^(BOOL finished)
     {
         self.timePickerView.hidden = YES;
         CGRect _smallHidden = self.timePickerView.frame;
         _smallHidden.origin.y = 276 + _smallHidden.size.height;
         self.timePickerView.frame = _smallHidden;
     }];
    [self.tableView reloadData];


}
//完成
-(void)done:(UIBarButtonItem*)item{
    
    
    
    
}


@end
